//
//  FinishViewController.m
//  CulinaryKnowledge
//
//  Created by CaoHung on 7/20/14.
//  Copyright (c) 2014 GreenSun. All rights reserved.
//

#import "FinishViewController.h"

#define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
#define IS_OS_7    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

@interface FinishViewController ()

@end

@implementation FinishViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"結果";
    if (IS_OS_7) {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backToHome)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    else{
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"タイトル" style:UIBarButtonItemStylePlain target:self action:@selector(backToHome)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame = CGRectMake(6, 515, 275, 51);
    [aButton addTarget:self action:@selector(goToshogakukan) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:aButton];
    
    UIButton *amazonButton = [UIButton buttonWithType:UIButtonTypeCustom];
    amazonButton.frame = CGRectMake(6, 582, 275, 51);
    [amazonButton addTarget:self action:@selector(goToAmazon) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:amazonButton];
    
    [self layoutScreen];
}

// Go to shogakukan shop
- (void)goToshogakukan
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.shogakukan.co.jp/books/detail/_isbn_9784093883511"]];
}

// Go to amazon
- (void)goToAmazon
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.amazon.co.jp/dp/4093883513"]];
}

// Layout Screen
- (void)layoutScreen
{
    self.resultTitle.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"result_Title.png"]];
    self.myScrollView.contentSize = CGSizeMake(291.0f, 989.0f);
    if (self.mPoints == 10) {
        self.lbPoints.text = @"";
        self.imageResultIcon.image = [UIImage imageNamed:@"result_Icon_10"];
    }else{
        self.imageResultIcon.image = [UIImage imageNamed:@"result_Icon"];
        self.lbPoints.text = [NSString stringWithFormat:@"%ld", (long)self.mPoints];
    }
    
    if (isiPhone5) {
        self.backgroundResult.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Questions4inch.png"]];
        if (IS_OS_7) {
            self.backgroundResult.frame = CGRectMake(15.0f, 102.0f, 291.0f, 457.5f);
        }
        else{
            self.backgroundResult.frame = CGRectMake(15.0f, 40.0f, 291.0f, 457.5f);
            self.resultTitle.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);
        }
    }
    else{
        // Set content frame
        CGRect contentFrame = self.contentResultImageView.frame;
        contentFrame.size.height = contentFrame.size.height + 70.0f;
        self.contentResultImageView.frame = contentFrame;
        
        self.backgroundResult.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Questions3.5inch.png"]];
        self.myScrollView.frame = CGRectMake(0.0f, 0.0f, 294.0f, 360.0f);
        if (IS_OS_7) {
            self.backgroundResult.frame = CGRectMake(15.0f, 101.0f, 291.0f, 369.0f);
        }
        else{
            self.resultTitle.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);
            self.backgroundResult.frame = CGRectMake(15.0f, 40.0f, 291.0f, 369.0f);
        }
    }
}

// Back to Home screen
- (void)backToHome
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
